package com.sinsoft.monitorDemo.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.JsonElement;
import redis.clients.jedis.Jedis;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sinsoft.monitorDemo.entity.NginxInfo;
import com.sinsoft.monitorDemo.util.JsonUtil;
import com.sinsoft.monitorDemo.util.RedisUtil;

public class NginxInfoDao {
	public List<NginxInfo> getNginxInfoList(String ip){
		Jedis jedis = RedisUtil.getJedis();
		List<String> jsonList = jedis.lrange("StatusData_" + ip + "_NGINX_latest", 0, -1);
		JsonArray jsonArray = null;
		JsonObject jsonObject = null;
		NginxInfo nginxInfo = null;
		List<NginxInfo> nginxInfoList = new ArrayList<NginxInfo>();
		if (jsonList.size()>10) {	
			for (int i = jsonList.size()-10; i < jsonList.size(); i++) {
				Map<Integer,String> map = new LinkedHashMap<Integer,String>();	
				jsonArray = JsonUtil.JsonParse(jsonList.get(i));
				jsonObject = jsonArray.get(0).getAsJsonObject();
				JsonArray array = jsonObject.get("port_status").getAsJsonArray();
				for (int j = 0; j < array.size(); j++) {
					JsonArray array2 = array.get(j).getAsJsonArray();
					int key = array2.get(0).getAsInt();
					String value = array2.get(1).getAsString();
					map.put(key, value);
				}
				nginxInfo = new NginxInfo(jsonObject.get("cpu_used_per")==null?0.0:jsonObject.get("cpu_used_per").getAsDouble(),
						jsonObject.get("max_connections").getAsInt(), jsonObject.get("status").getAsString(), 
						jsonObject.get("port_sum").getAsInt(),jsonObject.get("mem_used_per")==null?0.0:jsonObject.get("mem_used_per").getAsDouble(),
						jsonObject.get("connections").getAsInt(),map,jsonArray.get(1).getAsLong());			
				nginxInfoList.add(nginxInfo);
			}
		} else {
			for (int i = 1; i < jsonList.size(); i++) {
				Map<Integer,String> map = new LinkedHashMap<Integer,String>();	
				jsonArray = JsonUtil.JsonParse(jsonList.get(i));
				jsonObject = jsonArray.get(0).getAsJsonObject();
				JsonArray array = jsonObject.get("port_status").getAsJsonArray();
				for (int j = 0; j < array.size(); j++) {
					JsonArray array2 = array.get(j).getAsJsonArray();
					int key = array2.get(0).getAsInt();
					String value = array2.get(1).getAsString();
					map.put(key, value);
				}
				nginxInfo = new NginxInfo(jsonObject.get("cpu_used_per")==null?0.0:jsonObject.get("cpu_used_per").getAsDouble(),
						jsonObject.get("max_connections").getAsInt(), jsonObject.get("status").getAsString(), 
						jsonObject.get("port_sum").getAsInt(),jsonObject.get("mem_used_per")==null?0.0:jsonObject.get("mem_used_per").getAsDouble(),
						jsonObject.get("connections").getAsInt(),map,jsonArray.get(1).getAsLong());			
				nginxInfoList.add(nginxInfo);
			}
		}
		jedis.close();
		return nginxInfoList;		
	}
	
	/**
	 * 得到最新的Nginx信息
	 * @param ip
	 * @return
	 */
	public NginxInfo getNewNginxInfo(String ip){
		Jedis jedis = RedisUtil.getJedis();
		Map<Integer,String> map = new LinkedHashMap<Integer,String>();	
		NginxInfo nginxInfo;
		try {
			List<String> jsonList = jedis.lrange("StatusData_" + ip + "_NGINX_latest", -1, -1);
			JsonArray jsonArray = JsonUtil.JsonParse(jsonList.get(0));
			JsonObject jsonObject = jsonArray.get(0).getAsJsonObject();
			JsonArray array = jsonObject.get("port_status").getAsJsonArray();
			for (int j = 0; j < array.size(); j++) {
				JsonArray array2 = array.get(j).getAsJsonArray();
				int key = array2.get(0).getAsInt();
				String value = array2.get(1).getAsString();
				map.put(key, value);
			}
			nginxInfo = new NginxInfo(jsonObject.get("cpu_used_per")==null?0.0:jsonObject.get("cpu_used_per").getAsDouble(),
					jsonObject.get("max_connections").getAsInt(), jsonObject.get("status").getAsString(), 
					jsonObject.get("port_sum").getAsInt(),jsonObject.get("mem_used_per")==null?0.0:jsonObject.get("mem_used_per").getAsDouble(),
					jsonObject.get("connections").getAsInt(),map,
					jsonArray.get(1).getAsLong());
		} catch (Exception e) {
			return new NginxInfo(null, null, null, null, null, null, null, null);
		}finally{
			jedis.close();			
		}			
		return nginxInfo;		
	}

}
